<template>
  <div class="mod-sp__spuser">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-select placeholder="请选择" v-model="dataForm.roleId">
          <el-option value="" label="全部">全部</el-option>
          <el-option value="0" label="代表队负责人">代表队负责人</el-option>
          <el-option value="1" label="领队">领队</el-option>
          <el-option value="2" label="指导老师">指导老师</el-option>
          <el-option value="3" label="选手">选手</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.account" placeholder="账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.phone" placeholder="电话" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.email" placeholder="邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.realName" placeholder="实名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idCard" placeholder="身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.company" placeholder="单位" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <!--
      <el-form-item>
        <el-button v-if="hasPermission('sp:spuser:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spuser:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
      -->
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="account" label="账号" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="realName" label="姓名" header-align="center" align="center" width="100"></el-table-column>
      <el-table-column prop="idType" label="身份类型" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ getDictLabel("idType", scope.row.idType) }}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" header-align="center" align="center" width="150">
        <template v-slot="scope">
          {{ hideIdCard(scope.row.idCard) }}
        </template>
      </el-table-column>
      <el-table-column prop="company" label="单位" header-align="center" align="center" width="250"></el-table-column>
      <el-table-column prop="roleId" label="用户类型" header-align="center" align="center" width="180">
        <template v-slot="scope">
          {{ userType(scope.row.roleId) }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" header-align="center" align="center" width="200"></el-table-column>
      <el-table-column prop="phone" label="电话" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="sex" label="性别" header-align="center" align="center" width="60">
        <template v-slot="scope">
          {{ getDictLabel("gender", scope.row.sex) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <!--
          <el-button v-if="hasPermission('sp:spuser:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="hasPermission('sp:spuser:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button
          -->
          <el-button type="text" size="small" @click="detailHandle(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <detail v-if="detailVisible" ref="send" @refreshDataList="getDataList"></detail>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./spuser-add-or-update.vue";
import Detail from "./spuser-detail.vue";
import { IObject } from "@/types/interface";
export default defineComponent({
  components: {
    AddOrUpdate,
    Detail
  },
  setup() {
    const state = reactive({
      getDataListURL: "/sp/spuser/page",
      getDataListIsPage: true,
      exportURL: "/sp/spuser/export",
      deleteURL: "/sp/spuser",
      deleteIsBatch: true,
      limit: 20,
      dataForm: {
        roleId: "",
        account: "",
        phone: "",
        email: "",
        realName: "",
        idCard: "",
        company: ""
      },
      detailVisible: false
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  methods: {
    detailHandle(row: IObject) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.send.dataForm.row = row;
        this.$refs.send.init();
      });
    },
    userType(value?: string) {
      let result = "";
      if (value && value.includes("0")) {
        result += " 代表队负责人";
      }
      if (value && value.includes("1")) {
        result += " 领队";
      }
      if (value && value.includes("2")) {
        result += " 指导老师";
      }
      if (value && value.includes("3")) {
        result += " 选手";
      }
      return result;
    },
    /**
     * 身份证隐藏中间十位
     */
    hideIdCard(idCard: string): string {
      //验证身份证号必须满足18位，最后一位可以是字母
      if (!/^\d{17}(\d|x)$/i.test(idCard)) {
        return idCard;
      } else {
        return idCard.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
      }
    }
  }
});
</script>
